<?php 
namespace App\Console\Commands;

include base_path() . '/vendor/autoload.php';
use EasyWeChat\Foundation\Application;
use EasyWeChat\Message\Text;
use EasyWeChat\Message\Link;
use EasyWeChat\Message\News;
use Illuminate\Console\Command;
use Log;
use DB;
use App\WxPublicUser;
use App\WxMessage;

class CompleteUser extends Command {

    protected $config = [
        'app_id' => 'wx555be570d21859aa',
        'secret' => 'f92678be7687d1e1b1471ca9c066f242',
        'token' => 'leyougame2018'
    ];

    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'complete:user';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $app = new Application($this->config);
        $users = WxPublicUser::where(['nickname' => null])->get();
        foreach ($users as $user) {
            $userService = $app->user;
            $service = $userService->get($user->openid);
            $subscribe = $service->subscribe;
            if ($subscribe) {
                $info = $app->user->get($user->openid);
                $user->nickname = $info->nickname;
                $user->headimgurl = $info->headimgurl;
                $user->save();
            } else {
                $user->delete();                
            }
        }
    }

}